package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.*;
import com.bnz.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClassesMapper classesMapper;
    //列表所有学生
    @Override
    public List<Student> findAll() {

        //1.1 查询所有学生
        List<Student> students = studentMapper.selectByExample(null);
        //1.2 将学生与班级名称绑定
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }
        //1.3 返回学生列表
        return students;

    }

    //开始分页查询
    @Override
    public PageResult<Student> findByPage(int page, int pageSize) {
        //开始分页
        PageHelper.startPage(page,pageSize);

        //开始分页查询
        //定义实例
        StudentExample studentExample=new StudentExample();
        //开始查询
        List<Student> students=studentMapper.selectByExample(studentExample);
        //将学生关联的班级重新绑定
         for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }

        //转换
        Page<Student> pageInfo= (Page<Student>) students;
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getResult());
    }

    @Override
    public PageResult<Student> search(int page, int pageSize, Student student) {
        //开始分页
        PageHelper.startPage(page,pageSize);
        //开始分页查询
        //定义实例
        StudentExample studentExample=new StudentExample();

        //开始封装查询条件
        StudentExample.Criteria criteria=studentExample.createCriteria();
        if( student != null){
            if(StringUtils.isNotBlank(student.getSname())){
                criteria.andSnameLike("%" + student.getSname() + "%");
            }
            if(StringUtils.isNotBlank(student.getAddr())){
                criteria.andAddrLike("%" + student.getAddr() + "%");
            }
            if(student.getCid() != null && student.getCid() != 0){
                criteria.andCidEqualTo(student.getCid());
            }
        }

        //开始查询
        List<Student> students=studentMapper.selectByExample(studentExample);
        //将学生关联的班级重新绑定
         for (Student stud : students) {
            Classes classes = classesMapper.selectByPrimaryKey(stud.getCid());
            stud.setCname(classes.getCname());
        }
        //转换
        Page<Student> pageInfo= (Page<Student>) students;
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getResult());
    }


}
